package com.example.dllo.baidumusic.noitify;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.android.volley.Request;
import com.example.dllo.baidumusic.R;

import java.lang.reflect.Method;


/**
 * Created BaiDuMusic by *Vampire* on 16/8/25.
 */
public class PlayerNotification {
    private static final String TAG = "PlayerNotification";
    private NotificationManagerCompat notificationManagerCompat;
    private NotificationManager notificationManager;
    private Context context;
    private Notification notification;

    public PlayerNotification() {
    }

    public PlayerNotification(Context context) {
        this.context = context;
    }

    private Notification createNotify(String author, String song) {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.mipmap.ic_baidu);
        builder.setAutoCancel(false);// 点击不消失
        RemoteViews remoteViews = getRemoteViews(author, song);
        builder.setContent(remoteViews);


        return builder.build();
    }

    private RemoteViews getRemoteViews(String author, String song) {
        // 设置 自定义布局
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remtoe_notifycation);

        PendingIntent back = PendingIntent.getBroadcast(context, 001, new Intent("com.baidumusic.notification.back"), PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent next = PendingIntent.getBroadcast(context, 001, new Intent("com.baidumusic.notification.next"), PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent state = PendingIntent.getBroadcast(context, 001, new Intent("com.baidumusic.notification.state"), PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent clear = PendingIntent.getBroadcast(context, 001, new Intent("com.baidumusic.notification.clear"), PendingIntent.FLAG_CANCEL_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.notify_shang, back);
        remoteViews.setOnClickPendingIntent(R.id.notify_next, next);
        remoteViews.setOnClickPendingIntent(R.id.notify_play, state);


        remoteViews.setTextViewText(R.id.notify_singername, author);
        remoteViews.setTextViewText(R.id.notify_singname, song);


        return remoteViews;

    }

    public void showNotify(String author, String song) {


        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManagerCompat = NotificationManagerCompat.from(context);

        //获取NotifyCation对象
        notification = createNotify(author, song);
        // 设置滑动不取消
        notification.flags = Notification.FLAG_NO_CLEAR;
        // 设置单一通知
        Log.d(TAG, "showNotify");
        notificationManager.notify(0, notification);

    }

    public Notification getNotification() {
        return notification;
    }
}
